package note

import (
	"context"
	"errors"
	"gitee.com/CrazyMage/lite-note/internal/controller/http/v1/request"
	"gitee.com/CrazyMage/lite-note/internal/controller/http/v1/response"
	"gitee.com/CrazyMage/lite-note/internal/entity"
	"gitee.com/CrazyMage/lite-note/internal/repo"
	"gitee.com/CrazyMage/lite-note/pkg/utils"
)

type UseCase struct {
	repo repo.NoteRepo
}

func New(r repo.NoteRepo) *UseCase {
	return &UseCase{
		repo: r,
	}
}

func (uc *UseCase) CreateNote(ctx context.Context, req *request.ServiceCreateNoteReq) (resp response.ServiceCreateNoteResp, err error) {

	uid, err := utils.GetUserID(ctx)
	if err != nil {
		return response.ServiceCreateNoteResp{}, err
	}

	if uid != uint(req.Uid) {
		return response.ServiceCreateNoteResp{}, errors.New("不能创建他人笔记")
	}

	note := entity.Note{
		Uid:       uid,
		Title:     req.Title,
		Status:    req.Status,
		Content:   req.Content,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
	}

	err = uc.repo.CreateNote(&note)

	if err != nil {
		return response.ServiceCreateNoteResp{}, err
	}

	resp = response.ServiceCreateNoteResp{
		Note: response.Note{
			Uid:       uid,
			Title:     req.Title,
			Status:    req.Status,
			Content:   req.Content,
			StartTime: req.StartTime,
			EndTime:   req.EndTime,
		},
	}

	return resp, nil

}

func (uc *UseCase) ListNode(ctx context.Context, req *request.ServiceListNodeReq) (resp response.ServiceListNodeResp, err error) {
	uid, err := utils.GetUserID(ctx)

	if err != nil {
		return response.ServiceListNodeResp{}, errors.New("未登录")
	}

	if uid != uint(req.Uid) {
		return response.ServiceListNodeResp{}, errors.New("不能查看他人笔记")
	}

	// 获取当前用户的笔记列表
	notes, err := uc.repo.ListNodeByPages(uint(req.Uid), req.StartIndex, req.PageSize)
	if err != nil {
		return response.ServiceListNodeResp{}, err
	}

	// 转换为响应格式
	resp.NoteList = make([]response.Note, 0, len(*notes))
	for _, note := range *notes {
		resp.NoteList = append(resp.NoteList, response.Note{
			Uid:       note.Uid,
			Title:     note.Title,
			Status:    note.Status,
			Content:   note.Content,
			StartTime: note.StartTime,
			EndTime:   note.EndTime,
		})
	}

	return resp, nil

}

func (uc *UseCase) ShowNote(ctx context.Context, req *request.ServiceShowNoteReq) (resp response.ServiceShowNoteResp, err error) {
	uid, err := utils.GetUserID(ctx)
	if err != nil {
		return resp, errors.New("未登录")
	}

	// 获取笔记详情
	note, err := uc.repo.GetNoteById(int(req.Id))
	if err != nil {
		return resp, err
	}

	// 验证笔记是否属于当前用户
	if note.Uid != uid {
		return resp, errors.New("无权查看此笔记")
	}

	// 转换为响应格式
	resp.Note = response.Note{
		Uid:       note.Uid,
		Title:     note.Title,
		Status:    note.Status,
		Content:   note.Content,
		StartTime: note.StartTime,
		EndTime:   note.EndTime,
	}

	return resp, nil
}

func (uc *UseCase) DeleteNote(ctx context.Context, req *request.ServiceDeleteNoteReq) (resp response.ServiceDeleteNoteResp, err error) {
	uid, err := utils.GetUserID(ctx)
	if err != nil {
		return resp, errors.New("未登录")
	}

	// 先获取笔记详情以验证权限
	note, err := uc.repo.GetNoteById(int(req.Id))
	if err != nil {
		return resp, err
	}

	// 验证笔记是否属于当前用户
	if note.Uid != uid {
		return resp, errors.New("无权删除此笔记")
	}

	// 执行删除
	if err = uc.repo.DelNoteById(int(req.Id)); err != nil {
		return resp, err
	}

	// 返回被删除的笔记信息
	resp.Note = response.Note{
		Uid:       note.Uid,
		Title:     note.Title,
		Status:    note.Status,
		Content:   note.Content,
		StartTime: note.StartTime,
		EndTime:   note.EndTime,
	}

	return resp, nil
}

func (uc *UseCase) UpdateNote(ctx context.Context, req *request.ServiceUpdateNoteReq) (resp response.ServiceUpdateNoteResp, err error) {
	uid, err := utils.GetUserID(ctx)
	if err != nil {
		return resp, errors.New("未登录")
	}

	// 获取笔记详情以验证权限
	note, err := uc.repo.GetNoteById(int(req.Id))
	if err != nil {
		return resp, err
	}

	// 验证笔记是否属于当前用户
	if note.Uid != uid {
		return resp, errors.New("无权更新此笔记")
	}

	note.Title = req.Title
	note.Content = req.Content
	note.Status = req.Status
	note.StartTime = req.StartTime
	note.EndTime = req.EndTime

	// 执行更新
	err = uc.repo.UpdateNoteById(note)
	if err != nil {
		return resp, err
	}

	// 转换为响应格式
	note, err = uc.repo.GetNoteById(int(req.Id))
	if err != nil {
		return resp, err
	}

	resp.Note = response.Note{
		Uid:       note.Uid,
		Title:     note.Title,
		Status:    note.Status,
		Content:   note.Content,
		StartTime: note.StartTime,
		EndTime:   note.EndTime,
	}

	return resp, nil
}

func (uc *UseCase) SearchNote(ctx context.Context, req *request.ServiceSearchNoteReq) (resp response.ServiceSearchNoteResp, err error) {

	uid, err := utils.GetUserID(ctx)
	if err != nil {
		return resp, errors.New("未登录")
	}

	// 搜索标题中包含关键字的笔记
	notes, err := uc.repo.QueryByTitle(int(uid), req.Keyword)
	if err != nil {
		return resp, err
	}

	// 转换为响应格式
	resp.NoteList = make([]response.Note, 0, len(*notes))
	for _, note := range *notes {
		resp.NoteList = append(resp.NoteList, response.Note{
			Uid:       note.Uid,
			Title:     note.Title,
			Status:    note.Status,
			Content:   note.Content,
			StartTime: note.StartTime,
			EndTime:   note.EndTime,
		})
	}

	return resp, nil
}
